<template>
  <div class="feed">
    <div class="search">
      <el-form ref="form" :model="inlineForm" label-width="80px">
        <div class="top">
          <el-form-item>
            <el-input
              v-model="inlineForm.username"
              placeholder="反馈人"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="inlineForm.feedtext"
              placeholder="反馈内容"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="inlineForm.time"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </div>
        <div class="bottom">
          <div class="bottom_left">
            <el-form-item>
              <el-input
                v-model="inlineForm.feedresult"
                placeholder="反馈回复"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-select v-model="inlineForm.feedstatus" placeholder="反馈状态">
                <el-option label="已处理" :value="1"></el-option>
                <el-option label="未处理" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </div>
          <div class="bottom_btns">
            <el-button type="primary" icon="el-icon-search" @click="onSearch"
              >搜索</el-button
            >
            <el-button
              type="warning"
              icon="el-icon-refresh"
              @click="clearInlineForm"
              >重置</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete-solid"
              @click="clearAll"
              >批量删除</el-button
            >
            <el-button type="success" icon="el-icon-upload" @click="exportAll"
              >批量导出</el-button
            >
          </div>
        </div>
      </el-form>
    </div>
    <div class="table">
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column align="center" type="selection" width="55">
        </el-table-column>
        <el-table-column
          align="center"
          prop="username"
          label="反馈人"
          width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="feedtext"
          label="反馈内容"
          width="120"
        >
        </el-table-column>
        <el-table-column
          align="center"
          prop="feedtime"
          label="反馈时间"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.feedtime.split("T")[0] +
              " " +
              scope.row.feedtime.split("T")[1].split(".")[0]
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="feedresult"
          label="服务内容"
          align="center"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span v-if="scope.row.feedresult == null">暂无</span>
            <span v-else>{{ scope.row.feedresult }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="feedstatus"
          label="处理状态"
          show-overflow-tooltip
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.feedstatus == 1">已处理</span>
            <span v-else>未处理</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="feedscore"
          label="服务评分"
          show-overflow-tooltip
          align="center"
        >
          <template slot-scope="scope">
            <el-rate
              v-model="scope.row.feedscore"
              disabled
              show-score
              text-color="#ff9900"
              score-template="{value}"
            >
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column
          width="320px"
          prop="address"
          label="操作"
          show-overflow-tooltip
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              plain
              size="mini"
              @click="openDialog('反馈处理', scope.row, scope.$index)"
              >处理</el-button
            >
            <el-button
              type="primary"
              plain
              size="mini"
              @click="openDialog('反馈详情', scope.row, scope.$index)"
              >详情</el-button
            >
            <el-button
              type="primary"
              plain
              size="mini"
              @click="exportRow(scope.row, scope.$index)"
              >导出</el-button
            >
            <el-button
              type="warning"
              plain
              size="mini"
              @click="clearRow(scope.row, scope.$index)"
              >作废</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page" style="text-align: right">
      <el-pagination
        background=""
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <div class="modal">
      <el-dialog
        :title="this.diaTitle"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <el-form ref="form" :model="dialogForm" label-width="80px">
          <div class="top">
            <el-form-item label="反馈人">
              <el-input v-model="dialogForm.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="反馈时间">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dialogForm.time"
                  style="width: 100%"
                  disabled
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </div>
          <div class="bottom">
            <el-form-item label="反馈内容">
              <el-input
                type="textarea"
                v-model="dialogForm.feedtext"
                disabled
              ></el-input>
            </el-form-item>

            <el-form-item label="反馈结果">
              <el-input
                type="textarea"
                v-model="dialogForm.feedresult"
                :disabled="this.diaTitle == '反馈详情'"
              ></el-input>
            </el-form-item>
            <el-form-item label="反馈状态">
              <el-switch
                v-model="dialogForm.feedstatus"
                :active-value="1"
                :inactive-value="2"
                :disabled="this.diaTitle == '反馈详情'"
              ></el-switch>
            </el-form-item>
            <el-rate
              v-model="dialogForm.value"
              disabled
              show-score
              text-color="#ff9900"
              v-if="this.diaTitle == '反馈详情'"
            >
            </el-rate>
          </div>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button
            v-if="this.diaTitle == '反馈处理'"
            @click="dialogVisible = false"
            >取 消</el-button
          >
          <el-button
            type="primary"
            v-if="this.diaTitle == '反馈处理'"
            @click="reply"
            >回复</el-button
          >
          <el-button
            type="primary"
            @click="dialogVisible = false"
            v-if="this.diaTitle == '反馈详情'"
            >关闭</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { downloadXlsx } from "../../../../utils/xlsx";
export default {
  data() {
    return {
      //多选行
      multipleSelection: "",
      //对话框表单
      dialogForm: {
        username: "",
        time: "",
        feedtext: "",
        feedresult: "",
        feedstatus: "",
      },
      //对话框控制
      dialogVisible: false,
      diaTitle: "",
      dialogId: "",
      //日期范围
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      //顶部表单
      inlineForm: {
        username: "",
        feedtext: "",
        time: "",
        feedresult: "",
        feedstatus: "",
      },
      //分页
      total: 0,
      pageNumber: 1,
      pageSize: 10,

      //表格数据
      tableData: null,
    };
  },
  methods: {
    //删除单行
    clearRow(row, index) {
      this.$confirm("此操作将删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .get("poster/delFeedback", {
              params: {
                f_id: row.f_id,
                token: sessionStorage.getItem("token"),
              },
            })
            .then(() => {
              this.getTableData();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //导出单行
    exportRow(row, index) {
      let list = [
        ["反馈人", "反馈内容", "反馈时间", "服务内容", "处理状态", "服务评分"],
      ];

      let arr = [];
      arr.push(row.username);
      arr.push(row.feedtext);
      arr.push(row.feedtime);
      arr.push(row.feedresult);
      arr.push(row.feedstatus);
      arr.push(row.feedscore);
      list.push(arr);

      downloadXlsx(list, "单行.xlsx");
    },
    //批量导出
    exportAll() {
      let list = [
        ["反馈人", "反馈内容", "反馈时间", "服务内容", "处理状态", "服务评分"],
      ];
      for (let i = 0; i < this.multipleSelection.length; i++) {
        let arr = [];
        arr.push(this.multipleSelection[i].username);
        arr.push(this.multipleSelection[i].feedtext);
        arr.push(this.multipleSelection[i].feedtime);
        arr.push(this.multipleSelection[i].feedresult);
        arr.push(this.multipleSelection[i].feedstatus);
        arr.push(this.multipleSelection[i].feedscore);
        list.push(arr);
      }
      downloadXlsx(list, "多行.xlsx");
    },
    //批量删除
    clearAll() {
      if (this.multipleSelection == "") {
        this.$alert("请选择要删除的反馈数据", "提示", {
          confirmButtonText: "确定",
          callback: (action) => {
            this.$message({
              type: "info",
              message: `action: ${action}`,
            });
          },
        });
      } else {
        this.$confirm("确定删除选中的反馈内容？?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let ids = [];
            for (let i = 0; i < this.multipleSelection.length; i++) {
              ids.push(this.multipleSelection[i].f_id);
            }
            this.$axios
              .get("poster/delFeedbackData", {
                params: {
                  ids: ids,
                  token: sessionStorage.getItem("token"),
                },
              })
              .then(() => {
                this.getTableData();
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    //回复
    reply() {
      this.$axios
        .post("poster/updateFeedback", {
          f_id: this.dialogId,
          feedresult: this.dialogForm.feedresult,
          feedstatus: this.dialogForm.feedstatus,
          token: sessionStorage.getItem("token"),
        })
        .then(() => {
          this.dialogVisible = false;
          this.getTableData();
        });
    },
    //关闭对话框
    handleClose() {
      this.dialogVisible = false;
    },
    //打开对话框
    openDialog(tit, row, index) {
      //存id
      this.dialogId = row.f_id;

      this.diaTitle = tit;
      this.dialogVisible = true;
      this.dialogForm.username = row.username;
      this.dialogForm.time = row.feedtime;
      this.dialogForm.feedtext = row.feedtext;
      this.dialogForm.feedresult = row.feedresult;
      this.dialogForm.feedstatus = row.feedstatus;
    },
    //重置搜索栏
    clearInlineForm() {
      this.inlineForm = {
        username: "",
        feedtext: "",
        time: "",
        feedresult: "",
        feedstatus: "",
      };
    },
    //搜索
    onSearch() {
      this.$axios
        .get("poster/getFeedBack", {
          params: {
            username: this.inlineForm.username,
            feedtext: this.inlineForm.feedtext,
            startDate: this.inlineForm.time[0],
            endDate: this.inlineForm.time[1],
            feedresult: this.inlineForm.feedresult,
            feedstatus: this.inlineForm.feedstatus,
            currPage: this.pageNumber - 1,
            pageNum: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.data;
          this.total = res.data.totl;
          console.log(this.tableData, "tabledata");
        });
    },
    //多选
    handleSelectionChange(val) {
      // console.log(val,'val')
      this.multipleSelection = val;
    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber = val;
      this.getTableData();
    },
    //初始化表格
    getTableData() {
      this.$axios
        .get("/poster/getFeedBack", {
          params: {
            username: "",
            feedtext: "",
            startDate: "",
            endDate: "",
            feedresult: "",
            feedstatus: "",
            currPage: this.pageNumber - 1,
            pageNum: this.pageSize,
          },
        })
        .then((res) => {
          //   console.log(res);
          this.tableData = res.data.data;
          this.total = res.data.total;
        });
    },
  },
  created() {
    this.getTableData();
  },
};
</script>
<style lang="scss" scoped>
.feed::v-deep {
  .search {
    .el-form-item {
      width: 400px;
      margin-right: 10px;
      .el-form-item__content {
        margin-left: 0px !important;
      }
    }
    .top {
      display: flex;
      flex-direction: row;
    }
    .bottom {
      display: flex;
      flex-direction: row;
      .bottom_left {
        display: flex;
        flex-direction: row;
        .el-select {
          width: 398px;
        }
      }
    }
  }
  .table {
    thead {
      tr {
        th {
          font-size: 18px;
          background-color: #ddd;
          color: black;
        }
      }
    }
  }
  .modal {
    .top {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      .el-form-item {
        width: 45%;
      }
      .el-form-item:nth-child(2) {
        width: 45%;
        .el-form-item__content {
          width: 535px;
        }
      }
    }
  }
}
</style>